package tech.yunland.shop.web;

import com.moyuer.bsf.common.Pagination;
import com.moyuer.bsf.common.web.AuthNotRequired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tech.yunland.shop.domain.model.Goods;
import tech.yunland.shop.domain.model.GoodsPackage;
import tech.yunland.shop.domain.service.GoodsPackageService;
import tech.yunland.shop.domain.service.GoodsService;

import java.util.ArrayList;
import java.util.List;

/**
 * 微信商品
 * 
 * @author: 李珂<likenamehaoji@hotmail.com>
 */
@RestController
@RequestMapping("/weixin/goods")
@AuthNotRequired
public class WXGoodsController {

	@Autowired
	private GoodsService goodsService;

	@Autowired
	private GoodsPackageService goodsPackageService;



	/**
	 * 根据主键获取商品
	 * 
	 * @param id
	 * @Return Goods
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public Goods get(@PathVariable String id) {
		// 先更新使用频率
		if (!Goods.JF_ID.equals(id) && !Goods.DJQ_ID.equals(id)) {
			goodsService.updateRate(id);
		}

		Goods result = goodsService.get(id);

		return result;
	}




	/**
	 * 查询已上架的商品, 供选择商品用, 包含内置商品
	 * 
	 * @param param
	 *            参数
	 * @param tags
	 *            标签集
	 * @param page
	 *            起始索引
	 * @param rows
	 *            每页数量
	 * @return
	 * @Return List<Goods>
	 */
	@RequestMapping(value = "/on-shelf", method = RequestMethod.GET)
	public Pagination<Goods> getOnShelfGoods(
			@RequestParam(required = false) String param,
			@RequestParam(required = false) List<String> tags,
			@RequestParam(required = false) Integer page,
			@RequestParam(required = false) Integer rows,
			@RequestParam(required = false) List<String> oids) {

		List<Integer> states = new ArrayList<Integer>();
		states.add(1); // 上架状态

		Pagination<Goods> result = new Pagination<Goods>(page, rows);
		int startIdx = result.getStartIdx();

		int count = goodsService.getResultSize(param, states, tags, oids,true, null);
		List<Goods> records = goodsService.getResultInfos(param, states, tags, startIdx, rows, oids, true, null);

		result.setCount(count);
		result.setRecords(records);

		return result;
	}

	/**
	 * 分页读取优惠活动
	 *
	 * @param param
	 *            参数
	 * @param states
	 *            状态集
	 * @param page
	 *            起始索引
	 * @param rows
	 *            每页数量
	 * @return
	 * @Return Pagination<GoodsPackage>
	 */
	@RequestMapping(value = "/goods-package", method = RequestMethod.GET)
	public Pagination<GoodsPackage> getResultInfos(@RequestParam(required = false) String param
												  ,@RequestParam(required = false) List<Integer> states
			                                      ,@RequestParam(required = false) List<String> oids
			                                      ,@RequestParam(required = false) Integer page
												  ,@RequestParam(required = false) Integer rows) {

		Pagination<GoodsPackage> result = new Pagination<GoodsPackage>(page, rows);
		int startIdx = result.getStartIdx();

		int count = goodsPackageService.getResultSize(param, states, oids);
		List<GoodsPackage> records = goodsPackageService.getResultInfos(param, states, startIdx, rows, oids);

		result.setCount(count);
		result.setRecords(records);

		return result;
	}

}
